﻿using System.Collections.Generic;

using LightningTools.BCSTesterMan.Model;
using LightningTools.BCSTesterMan.Model.DataAccessLayer;

namespace LightningTools.BCSTesterMan.ViewModel
{
    public class MethodViewModel : TreeViewItemViewModel
    {
        private List<Parameter> inputParameters;
        private Method method;

        /// <summary>
        /// Initializes a new instance of the <see cref="MethodViewModel"/> class.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <param name="entityViewModel">The entity view model.</param>
        public MethodViewModel(Method method, TreeViewItemViewModel entityViewModel)
            : base(entityViewModel, false)
        {
            Method = method;
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get { return method.Name; }
        }

        /// <summary>
        /// Gets or sets the method.
        /// </summary>
        /// <value>The method.</value>
        public Method Method
        {
            get { return method; }
            set { method = value; }
        }

        /// <summary>
        /// Gets a value indicating whether method type is supported.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is supported; otherwise, <c>false</c>.
        /// </value>
        public bool IsSupported
        {
            get { return method.IsSupported; }
        }

        /// <summary>
        /// Gets or sets the input parameters.
        /// </summary>
        /// <value>The input parameters.</value>
        public List<Parameter> InputParameters
        {
            get { return inputParameters; }
            set
            {
                inputParameters = value;
                OnPropertyChanged("InputParameters");
            }
        }

        /// <summary>
        /// Called when property changed.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected override void OnPropertyChanged(string propertyName)
        {
            if (propertyName != null)
            {
                if (propertyName == "IsSelected" && IsSelected)
                {
                    var viewModel = Parent as EntityViewModel;

                    if (viewModel != null)
                    {
                        // Load the Parameters for the Method
                        if (InputParameters == null)
                        {
                            InputParameters = new List<Parameter>(BcsHelper.GetInputParameters(method, viewModel.BcsUrl));
                        }
                    }
                }
            }
            base.OnPropertyChanged(propertyName);
        }
    }
}